package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.mapper.MemberMapper;
import com.li.pojo.Member;
import com.li.service.MemberService;
import com.li.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberMapper memberMapper;

    public Member findMemberByPhone(String telephone) {
        //通过手机号登录
       return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {

        //新增会员 为密码进行加密
        if(member.getPassword()!=null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberMapper.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> list) {
        //获取每个月截止的会员人数
        ArrayList<Integer> lists = new ArrayList<>();

        for (String month : list) {
            month = month + ".31";
            Integer count = memberMapper.findMemberCountBeforeDate(month);
            lists.add(count);
        }

        return lists;
    }


    //男女会员占比
    @Override
    public List<Map<String, Object>> findMemberBySex() {
        return memberMapper.findMemberBySex();
    }

    //会员年龄段占比
    @Override
    public List<Map<String, Object>> findMemberByAge(String arg0,String arg1) {
        return memberMapper.findMemberByAge(arg0,arg1);
    }



    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

            Integer currentPage = queryPageBean.getCurrentPage();
            Integer pageSize = queryPageBean.getPageSize();
            String queryString = queryPageBean.getQueryString();
            PageHelper.startPage(currentPage,pageSize);
            Page<Member> memberPage = memberMapper.findByCondition(queryString);
            long total = memberPage.getTotal();
            List<Member> rows = memberPage.getResult();
            return new PageResult(total,rows);

    }

    @Override
    public Member findMemberById(int id) {
        Member me = memberMapper.findMemberById(id);
        return me;
    }

    @Override
    public void edit(Member member) {
        memberMapper.edit(member);
    }

    @Override
    public void delete(int id) {
        memberMapper.delete(id);
    }


}
